home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / linux / tools / gtar10.lha / names.c < prev    next >
C/C++ Source or Header  |  1992-09-09  |  3KB  |  141 lines

  1. /* Look up user and/or group names.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Look up user and/or group names.
  22.  *
  23.  * This file should be modified for non-unix systems to do something
  24.  * reasonable.
  25.  *
  26.  * @(#)names.c 1.3 10/30/87 - gnu
  27.  */ 
  28. #include <sys/types.h>
  29. #include "tar.h"
  30.  
  31. #ifdef amigados
  32. static inline int setgrent() { return 0;}
  33. static inline struct group *getgrnam(const char *a) { return (struct group *) getgrgid (0); }
  34. #endif
  35.  
  36. extern    char    *strncpy();
  37.  
  38. #ifndef NONAMES
  39. /* Whole module goes away if NONAMES defined.  Otherwise... */
  40. #include <pwd.h>
  41. #include <grp.h>
  42.  
  43. static int    saveuid = -993;
  44. static char    saveuname[TUNMLEN];
  45. static int    my_uid = -993;
  46.  
  47. static int    savegid = -993;
  48. static char    savegname[TGNMLEN];
  49. static int    my_gid = -993;
  50.  
  51. #define myuid    ( my_uid < 0? (my_uid = getuid()): my_uid )
  52. #define    mygid    ( my_gid < 0? (my_gid = getgid()): my_gid )
  53.  
  54. /*
  55.  * Look up a user or group name from a uid/gid, maintaining a cache.
  56.  * FIXME, for now it's a one-entry cache.
  57.  * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  58.  *
  59.  * This is ifdef'd because on Suns, it drags in about 38K of "yellow
  60.  * pages" code, roughly doubling the program size.  Thanks guys.
  61.  */
  62. void
  63. finduname(uname, uid)
  64.     char    uname[TUNMLEN];
  65.     int    uid;
  66. {
  67.     struct passwd    *pw;
  68.     extern struct passwd *getpwuid ();
  69.  
  70.     if (uid != saveuid) {
  71.         saveuid = uid;
  72.         saveuname[0] = '\0';
  73.         pw = getpwuid(uid); 
  74.         if (pw) 
  75.             strncpy(saveuname, pw->pw_name, TUNMLEN);
  76.     }
  77.     strncpy(uname, saveuname, TUNMLEN);
  78. }
  79.  
  80. int
  81. finduid(uname)
  82.     char    uname[TUNMLEN];
  83. {
  84.     struct passwd    *pw;
  85.     extern struct passwd *getpwnam();
  86.  
  87.     if (uname[0] != saveuname[0]    /* Quick test w/o proc call */
  88.         || 0!=strncmp(uname, saveuname, TUNMLEN)) {
  89.         strncpy(saveuname, uname, TUNMLEN);
  90.         pw = getpwnam(uname); 
  91.         if (pw) {
  92.             saveuid = pw->pw_uid;
  93.         } else {
  94.             saveuid = myuid;
  95.         }
  96.     }
  97.     return saveuid;
  98. }
  99.  
  100.  
  101. void
  102. findgname(gname, gid)
  103.     char    gname[TGNMLEN];
  104.     int    gid;
  105. {
  106.     struct group    *gr;
  107.     extern struct group *getgrgid ();
  108.  
  109.     if (gid != savegid) {
  110.         savegid = gid;
  111.         savegname[0] = '\0';
  112.         (void)setgrent();
  113.         gr = getgrgid(gid); 
  114.         if (gr) 
  115.             strncpy(savegname, gr->gr_name, TGNMLEN);
  116.     }
  117.     (void) strncpy(gname, savegname, TGNMLEN);
  118. }
  119.  
  120.  
  121. int
  122. findgid(gname)
  123.     char    gname[TUNMLEN];
  124. {
  125.     struct group    *gr;
  126.     extern struct group *getgrnam();
  127.  
  128.     if (gname[0] != savegname[0]    /* Quick test w/o proc call */
  129.         || 0!=strncmp(gname, savegname, TUNMLEN)) {
  130.         strncpy(savegname, gname, TUNMLEN);
  131.         gr = getgrnam(gname); 
  132.         if (gr) {
  133.             savegid = gr->gr_gid;
  134.         } else {
  135.             savegid = mygid;
  136.         }
  137.     }
  138.     return savegid;
  139. }
  140. #endif
  141.